//
//  TableViewItemCell.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2018/12/20.
//  Copyright © 2018 Mr.wang. All rights reserved.
//

import UIKit

class TableViewItemCell: UITableViewCell {
    
    let sepatorView = UIView()
    var item: TableViewItem!
    var textField: UITextField?
    
    let leftLabel = UILabel()
    let rightLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = .white
        
        leftLabel.textColor = .black
        leftLabel.setContentHuggingPriority(.required, for: .horizontal)
        leftLabel.font = UIFont.systemFont(ofSize: 15)
        contentView.addSubview(leftLabel)
        leftLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
        }
        
        rightLabel.textColor = #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
        rightLabel.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(rightLabel)
        rightLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-5)
        }
        
        sepatorView.isHidden = true
        sepatorView.backgroundColor = #colorLiteral(red: 0.9725490196, green: 0.9725490196, blue: 0.9725490196, alpha: 1)
        addSubview(sepatorView)
        sepatorView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if selected {
            self.setSelected(false, animated: true)
        }
    }
    
    func update(_ model: TableViewItem) {
        self.item = model
        
        self.selectionStyle = model.selectedStyle
        self.accessoryView = nil
        
        leftLabel.text = model.title
        rightLabel.text = model.detailTitle
        
        textField?.removeFromSuperview()
        textField = nil
        
        switch model.accessoryType {
        case .none: self.accessoryType = .none
        case .indicator: self.accessoryType = .disclosureIndicator
            break
        }
        
        sepatorView.isHidden = item.sepatorType == .none
        
        if let textFieldClosure = item.textFieldSetUp {
            textField = UITextField()
            textField?.textColor = #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
            textField?.font = UIFont.systemFont(ofSize: 14)
            textField?.textAlignment = .right
            textFieldClosure(textField!)
            textField?.text = item.textFieldValue
            contentView.addSubview(textField!)
            textField!.snp.makeConstraints { (make) in
                make.top.bottom.equalTo(0)
                make.right.equalTo(-5)
                make.left.equalTo(leftLabel.snp.right).offset(20)
            }
            
            textField!.rx.text.orEmpty
                .bind { [unowned self] text in
                    self.item.textFieldValue = text
            }.disposed(by: rx.disposeBag)
        }
    }
    
    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.size.width, height: item.cellHeight)
    }
    
}
